﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Net;


namespace AutoMeasureV2._1
{
    /// <summary>
    /// MessageSet.xaml 的交互逻辑
    /// </summary>
    public partial class MessageBand : Window
    {
        public MessageBand()
        {
            InitializeComponent();
            mesSet();
        }
        public iniInfo _info = new iniInfo();

        /// <summary>
        /// 界面信息设置
        /// </summary>
        public void mesSet()
        {
            _info.Info();

            foreach (var item in _info.Communications)    //comboBox信息加载
            {
                objComboBox.Items.Add(item.Name);
            }

            foreach (var item in _info.ResSins)          //mesListBox信息加载
            {
                mesListBox.Items.Add(item.Name);
            }
        }

        #region 状态变更

        private void objComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in _info.Communications)
            {
                if (item.Name == objComboBox.SelectedValue.ToString())
                {
                    objListBox.Items.Clear();
                    foreach (var sin in item.Sins)
                    {
                        objListBox.Items.Add(sin.objName);
                    }
                    break;
                }
            }
        }

        private void mesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (_info._band.Count != 0)
                {
                    foreach (var item in _info._band)
                    {
                        if (item.Value.MesName == mesListBox.SelectedValue.ToString())
                        {
                            MesTextBox.Text = item.Value.ObjName;
                            mesTextBlock.Text = "该资源已被绑定";
                            break;
                        }
                        else
                        {
                            mesTextBlock.Text = "资源尚未绑定，请选择";
                            MesTextBox.Text = string.Empty;
                        }
                    }
                }
                else
                {
                    mesTextBlock.Text = "资源尚未绑定，请选择";
                    MesTextBox.Text = string.Empty;
                }
            }
            catch
            {
                MessageBox.Show("配置信息错误");
            }
        }

        private void objListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                foreach (var item in _info._band.Values)
                {
                    if (objListBox.SelectedValue != null)
                    {
                        if (item.ObjName == objListBox.SelectedValue.ToString())
                        {
                            mesTextBlock.Text = "该对象已被绑定到：‘" + item.MesName + "’ 无法再次绑定";
                            break;
                        }
                        else
                            mesTextBlock.Text = "该对象尚未绑定，请选择";
                    }
                }

            }
            catch
            {
                MessageBox.Show("配置信息错误");
            }

        }

        #endregion

        #region 三个按钮设定

        private void bandButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (mesListBox.SelectedValue != null && objListBox.SelectedValue != null)
                {
                    string ResName = mesListBox.SelectedValue.ToString();
                    string ObjName = objListBox.SelectedValue.ToString();

                    int isOK = 0;                  //0 未被绑定，1已被绑定

                    if (_info._band != null)
                    {
                        foreach (var item in _info._band)
                        {
                            if (ResName == item.Value.MesName || ObjName == item.Value.ObjName)
                            {
                                if (ResName == item.Value.MesName)
                                    mesTextBlock.Text = "资源已被绑定，绑定失败";
                                else
                                    mesTextBlock.Text = "对象已被绑定，绑定失败";
                                isOK = 1;
                                break;
                            }
                        }
                    }

                    if (isOK == 0)
                    {
                        Band band = new Band();
                        foreach (var resItem in _info.ResSins)
                        {
                            if (resItem.Name == mesListBox.SelectedValue.ToString())
                            {
                                band.MesName = resItem.Name;
                                band.MesID = resItem.ID;
                                foreach (var objitem in _info.Communications)
                                {
                                    foreach (var objitems in objitem.Sins)
                                    {
                                        if (objitems.objName == objListBox.SelectedValue.ToString())
                                        {
                                            band.ObjSign = objitem.Sign;
                                            band.ObjPort = objitem.RemotePort;
                                            band.ObjIP = objitem.RemoteIP;
                                            band.ObjName = objitems.objName;
                                            band.ObjID = objitems.objID;
                                            band.ObjOrder = objitems.objOrder;
                                            band.ObjRespond = objitems.objRespond;
                                            band.ObjDigit = objitems.objDigit;

                                            MesTextBox.Text = objitems.objName;
                                            _info.AddBand(band);
                                            break;
                                        }
                                    }
                                }

                                ListBoxItem item = mesListBox.ItemContainerGenerator.ContainerFromIndex(mesListBox.SelectedIndex) as ListBoxItem;
                                item.FontWeight = FontWeights.Black;
                                mesTextBlock.Text = "绑定成功";

                                break;
                            }
                        }
                    }

                }
                else
                    mesTextBlock.Text = "绑定失败";
            }
            catch
            {
                MessageBox.Show("绑定信息错误");
                this.Close();
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (_info._band.Count != 0)
            {
                foreach (var item in _info._band)
                {
                    if (item.Value.MesName == mesListBox.SelectedValue.ToString())
                    {
                        _info.removeBand(item.Value.MesName);
                        ListBoxItem items = mesListBox.ItemContainerGenerator.ContainerFromIndex(mesListBox.SelectedIndex) as ListBoxItem;
                        if (items != null)
                            items.FontWeight = FontWeights.Light;
                        mesTextBlock.Text = "解绑成功";
                        MesTextBox.Text = string.Empty;
                        break;
                    }
                    else
                        mesTextBlock.Text = "解绑失败";
                }
            }
            else
                mesTextBlock.Text = "解绑失败";
        }

        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in _info.ResSins)
            {
                ListBoxItem boxitem = mesListBox.ItemContainerGenerator.ContainerFromIndex(item.Num) as ListBoxItem;
                if (boxitem != null)
                    boxitem.FontWeight = FontWeights.Light;
            }
            _info.clear();
            mesTextBlock.Text = "绑定信息已清空";
            MesTextBox.Text = string.Empty;
        }

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (_info._band != null)
            {
                // 已绑定项，字体加粗
                foreach (var item in _info._band)
                {
                    foreach (var child in _info.ResSins)
                    {
                        if (child.Name == item.Value.MesName)
                        {
                            ListBoxItem boxItem = mesListBox.ItemContainerGenerator.ContainerFromIndex(child.Num) as ListBoxItem;
                            if (boxItem != null)
                                boxItem.FontWeight = FontWeights.Black;
                            break;
                        }
                    }
                }
            }

        }

    }

}
